import javax.crypto.SecretKey;
import java.util.Base64;
/**
 * 测试密码学用的主函数
 */
public class Main {

    public static void testAES() throws Exception{
        System.out.println("*******AES加密测试开始*********");
        String plaintext = "恰同学少年，风华正茂；书生意气，挥斥方遒。指点江山，激扬文字，粪土当年万户侯。曾记否，到中流击水，浪遏飞舟？";
        String key = "卜算子-咏梅";
//        System.out.println(key.getBytes().length);
        System.out.println("明文是："+ plaintext+"; 密钥是："+ key);
        AESCipher aesCipher = new AESCipher();
        byte[] ciphertext = aesCipher.encrypt(key.getBytes(), plaintext.getBytes());
        System.out.println("密文（进行了Base64编码）: " + Base64.getEncoder().encodeToString(ciphertext));
        byte[] decryptedtext = aesCipher.decrypt(key.getBytes(), ciphertext);
        System.out.println("解密后的明文是: " + new String(decryptedtext));
        System.out.println("*******AES加密测试结束*********");
    }

    public static void testDES() throws Exception {
        System.out.println("*******DES加密测试开始*********");
        String plaintext = "故人西辞黄鹤楼，烟花三月下扬州。孤帆远影碧空尽，唯见长江天际流。";
        String key = "intheend";
        System.out.println("明文是："+ plaintext+"; 密钥是："+ key);
        DESCipher desCipher = new DESCipher();
        // 加密
        byte[] encryptedString = desCipher.encrypt(plaintext.getBytes(), key.getBytes());
        System.out.println("密文（进行了Base64编码）: " + Base64.getEncoder().encodeToString(encryptedString));
        // 解密
        byte[] decryptedString = desCipher.decrypt(encryptedString, key.getBytes());
        System.out.println("解密后的明文是: " + new String(decryptedString));
        System.out.println("*******DES加密测试结束*********");
    }

    public static void testRC4Cipher(){
        System.out.println("*******RC4加密测试开始*********");
        RC4Cipher rc4 = new RC4Cipher();
        String plaintext = "在屋顶唱着你的歌, 在屋顶和我爱的人, 让星星点缀成最浪漫的夜晚, 拥抱这时刻这一分一秒全都停止。";
        String key = "QQ音乐";
        System.out.println("明文是："+ plaintext+"; 密钥是："+ key);
        byte []byte_ct = rc4.encrypt(key.getBytes(), plaintext.getBytes());
        System.out.println("密文是：" + rc4.bytesToHex(byte_ct));
        byte []byte_pt = rc4.decrypt(key.getBytes(), byte_ct);
        System.out.println("解密后的明文是："+new String(byte_pt));
        System.out.println("*******RC4加密测试结束*********");
    }
    /**
     * 凯撒加密测试
     */
    public static void testCasesarCipher(){
        System.out.println("*******凯撒加密测试开始*********");
        CaesarCipher caesarCipher = new CaesarCipher();
        String plaintext = "We were both young when I first saw you I close my eyes and the flashback starts I'm standing there on a balcony in summer air See the lights see the party the ball gowns See you make your way through the crowd";
        int key = 7;
        System.out.println("明文是："+ plaintext+"; 密钥是："+ key);
        String ct = caesarCipher.encrypt(key , plaintext);
        System.out.println("密文是：" + ct);
        String pt = caesarCipher.decrypt(key, ct);
        System.out.println("解密后的明文是："+pt);
        System.out.println("*******凯撒加密测试结束*********");
    }

    public static void testAffineCipher(){
        System.out.println("*******仿射加密测试开始*********");
        int a = 17, b = -8, m = 26;
        AffineCipher affineCipher = new AffineCipher(a, b, m);
        String plaintext = "affineshift";
        System.out.println("明文是："+ plaintext+"; 密钥是:a="+ String.valueOf(a)+";b="+String.valueOf(b)+";m="+String.valueOf(m));
        String ct = affineCipher.encrypt(plaintext);
        System.out.println("密文是：" + ct);
        String pt = affineCipher.decrypt(ct);
        System.out.println("解密后的明文是："+pt);
        System.out.println("*******仿射加密测试结束*********");
    }

    public static void testVigenereCipher(){
        System.out.println("*******维吉尼亚加密测试开始*********");
        VigenereCipher vigenereCipher = new VigenereCipher();
        String plaintext = "I'm sitting here in the boring room It's just another rainy Sunday afternoon I'm wasting my time I got nothing to do I'm hanging around I'm waiting for you";
        String key = "key";
        System.out.println("明文是："+ plaintext+"; 密钥是："+ key);
        String ct = vigenereCipher.encrypt(key , plaintext);
        System.out.println("密文是：" +ct);
        String pt = vigenereCipher.decrypt("key", ct);
        System.out.println("解密后的明文是：" + pt);
        System.out.println("*******维吉尼亚加密测试结束*********");

    }
    public static void main(String[] args) throws Exception {
        testCasesarCipher();
        testVigenereCipher();
        testAffineCipher();
        testRC4Cipher();
        testDES();
        testAES();
    }
}
